<template>
  <a-input-number
    style="width: 100%"
    :max="max"
    :parser="parser"
    :formatter="formatter"
    v-bind="$attrs"
    v-on="$listeners"
  />
</template>

<script>
export default {
  name: 'CInputNumber',
  props: {
    precision: {
      type: Number,
      default: 6
    },
    prefix: {
      type: String,
      default: ''
    },
    suffix: {
      type: String,
      default: ''
    },
    max: {
      type: Number,
      default: 999999999.999999
    }
  },
  model: {
    prop: 'value',
    event: 'change'
  },
  methods: {
    parser(string) {
      if (string) {
        let value = string
        if (this.suffix) {
          value = value.replace(this.suffix, '')
        }
        if (this.prefix) {
          value = value.replace(this.prefix, '')
        }
        const [int, float] = value.split('.')
        if (float && float.length > this.precision) {
          return int + '.' + float.substr(0, this.precision)
        }
        return value
      }
    },
    formatter(value) {
      if (value) {
        const [int, float] = value.toString().split('.')
        if (float && float.length > this.precision) {
          return this.prefix + int + '.' + float.substr(0, this.precision) + this.suffix
        }
        return this.prefix + value + this.suffix
      }
    }
  }
}
</script>

<style></style>
